package controller;

import dao.BaseMapper;
import org.springframework.web.bind.annotation.*;
import pojo.*;
import response.Result;
import service.CustomerService;
import service.SoMainService;
import utils.DateUtil;
import utils.IDCreateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘星宇
 * @ClassName 刘星宇
 * @Description TODO
 * @date 2022/12/2 14:21
 * @Version 1.0
 */
@RestController
public class SoMainController {
    @Resource
    private SoMainService soMainService;
    @Resource
    private CustomerService customerService;


    @RequestMapping("getSomains")
    public Result getSomains(Integer currentPage, Integer pageSize, HttpServletRequest request) {
        Page<Somain> s = new Page<Somain>();
        s.setCurrentPage(currentPage);
        s.setPageSize(pageSize);
        Somain so = new Somain();
        SCMUser u = (SCMUser) request.getSession().getAttribute("loginUser");
        so.setAccount(u.getAccount());
        int totalRecord = soMainService.getCount(so);
        List<Somain> somainList = soMainService.getAllByPage(s, so);
        s.setLists(somainList);
        s.setTotalRecord(totalRecord);
        return Result.ok(s);
    }

    //查询基础查询
    @GetMapping("getBasicMassage1")
    public Result getBasicMassage1(HttpServletRequest request) {
        Somain so = new Somain();
        SCMUser u = (SCMUser) request.getSession().getAttribute("loginUser");
        so.setAccount(u.getAccount());
        so.setCreateTime(DateUtil.getDateTime());
        so.setSoId(new BigDecimal(IDCreateUtil.getId()));
        return Result.ok(so);
    }

    //查出客户的信息
    @GetMapping("getCustomerMassage")
    public Result getCustomerMassage() {
        List<Customer> customerList = customerService.getAll1();
        for (Customer customer : customerList) {
            System.out.println(customer);
        }
        return Result.ok(customerList);
    }

    //新增采购单
    @PostMapping("addOrUpdateSoMain")
    public Result addSoMain(@RequestBody Somain somain) {
        if (soMainService.updateSoMainBySoID(somain.getSoId())==null) {
            soMainService.add(somain);
        }else {
            soMainService.del(somain);
            soMainService.add(somain);
        }
        return Result.ok();
    }
    //修改采购单的页面数据回填
    @GetMapping("getSoMainBySoId")
    public Result getSoMainBySoId(String soId){
        Somain somain = soMainService.getSoMainBySoID(soId);
        return Result.ok(somain);
    }
    @GetMapping("getSoItemsBySoId/{soId}")
    public Result getSoItemsBySoID(@PathVariable("soId") String soId) {
        List<Soitem> soitemList =soMainService.getSoItemsBySoID(soId);
        return Result.ok(soitemList);
    }
    @GetMapping ("delSomain")
    public Result delSomain(Somain soId){
        soMainService.del(soId);
        return Result.ok();
    }
    //了结
    @RequestMapping("getSoMainPage")
    public Page<Somain> getSoMainPage(int currentPage, int pageSize,Somain somain,int payType,HttpServletRequest req){
        SCMUser scm =(SCMUser)req.getSession().getAttribute("loginUser");
        somain.setAccount(scm.getAccount());
        Page<Somain> somainPage = soMainService.getSoMainPage(new Page<Somain>(currentPage,pageSize,0,null),somain,payType);
        return somainPage;
    }
    //出库
    @RequestMapping("getOnboundPage")
    public Page<Somain> getOnboundPage(int currentPage, int pageSize,Somain somain,int payType,HttpServletRequest req){
        SCMUser scm =(SCMUser)req.getSession().getAttribute("loginUser");
        somain.setAccount(scm.getAccount());
        Page<Somain> somainPage = soMainService.onboundPage(new Page<Somain>(currentPage,pageSize,0,null),somain,payType);
        return somainPage;
    }
    //确认出库？
    @RequestMapping("updateOutbound")
    public Result updateOutbound(BigDecimal soId,HttpServletRequest req){
        SCMUser u=(SCMUser)req.getSession().getAttribute("loginUser");
        Somain somain = new Somain();
        somain.setStockUser(u.getName());
        somain.setSoId(soId);
        soMainService.updOutbound(somain);
        return Result.ok();
    }
    @PostMapping("updateStatus1")
    public Result updateStatus1(@RequestBody Somain somain){
        soMainService.endUpd1(somain);
        return Result.ok();
    }
    @RequestMapping("getSomain")
    public Result getSomain(Integer currentPage, Integer pageSize,BigDecimal soId,String createTime,String endTime,String customerCode,int payType,int status,HttpServletRequest req){
        Page<Somain> page =new Page<Somain>();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        SCMUser scm =(SCMUser)req.getSession().getAttribute("loginUser");
        Somain so  = new Somain();
        so.setSoId(soId);
        so.setCreateTime(createTime);
        so.setEndTime(endTime);
        so.setCustomerCode(customerCode);
        so.setPayType(payType);
        so.setStatus(status);
        so.setAccount(scm.getAccount());
        int totalRecord = soMainService.getSoMainCount(page,so);
        List<Somain> list = soMainService.somainAllByPage(page,so);
        page.setLists(list);
        page.setTotalRecord(totalRecord);
        return Result.ok(page);
    }
    @RequestMapping("getInMoneyPage")
    public Page<Somain> getInMoneyPage(int currentPage, int pageSize,Somain somain,int payType,HttpServletRequest req){
        SCMUser scm =(SCMUser)req.getSession().getAttribute("loginUser");
        somain.setAccount(scm.getAccount());
        Page<Somain> somainPage = soMainService.inMoneyPage(new Page<Somain>(currentPage,pageSize,0,null),somain,payType);
        return somainPage;
    }
    @RequestMapping("updateInMoney")
    public Result updateInMoney(BigDecimal soId,HttpServletRequest req){
        SCMUser u=(SCMUser)req.getSession().getAttribute("loginUser");
        Somain somain = new Somain();
        somain.setPayUser(u.getName());
        somain.setSoId(soId);
        soMainService.updateInMoney(somain);
        return Result.ok();
    }
    @RequestMapping("querySoReport")
    public Result querySoReport(Somain somain,String date,HttpServletRequest req){
        Page<Somain> page =new Page<Somain>();
        SCMUser u=(SCMUser) req.getSession().getAttribute("loginUser") ;
        somain.setAccount(u.getName());
        page.setCurrentPage(somain.getCurrentPage());
        page.setPageSize(somain.getPageSize());
        Somain so =new Somain();
        so.setDate(date);
        int totalrecord=soMainService.soReportCount(page,somain);
        List<Somain> somianList = soMainService.soReportPage(page,somain);
        for (Somain s :somianList){
            if (s.getStatus()!=4 && s.getStatus() !=5){
                s.setUnPay(s.getSoTotalPrice());
            }
            if (s.getStatus()==5){
                s.setUnPay(s.getSoTotalPrice()-s.getPrePayFee());
            }
            if (s.getStatus()==4){
                s.setUnPay((float)0);
            }
        }
        page.setTotalRecord(totalrecord);
        page.setLists(somianList);
        return Result.ok(page);
    }
    @RequestMapping("querySoAllCount")
    public List<Somain> querySoAllCount(Somain somain,String date){
        int i = 0;//计算了结单
        float sum=0;
        float psum=0;
        somain.setDate(date);
        List<Somain> somianList = soMainService.soReportPage1(somain);
        for (Somain so: somianList){
            if (so.getStatus()==4){
                i++;
            }
            sum += so.getSoTotal();
            if (so.getPayType()==1){
                if (so.getStatus()<3){
                    psum+=0;
                }
                if (so.getStatus()>=3){
                    psum+=so.getSoTotal();
                }
            }
            if (so.getPayType()==2){
                if (so.getStatus()==1){
                    psum+=0;
                }
                if (so.getStatus()>1){
                    psum+=so.getSoTotal();
                }
            }
            if (so.getPayType()==3){
                if (so.getStatus()==1){
                    psum+=0;
                }
                if (so.getStatus()==5 || so.getStatus()==2){
                    psum+=so.getPrePayFee();
                }
                if (so.getStatus()==3 || so.getStatus()==4){
                    psum+=so.getSoTotal();
                }
            }
        }
        int totalrecord=soMainService.soReportCount1(somain);
        somain.setSoMainCount(totalrecord);
        somain.setEndCount(i);
        somain.setSoTotalPrice(sum);
        somain.setPayPrice(psum);
        List<Somain> list =new ArrayList<>();
        list.add(somain);
        return list;
    }
}
